<?php
namespace Xuan\Controller;
class ShopController extends CommonController {

    public function lists() {
        $keywords = trim(I('get.keywords'));
        $sql = "1 = 1 ";
        if ($keywords != '') {
            $sql .= " AND name like '%" . $keywords . "%'";
        }
        $count = M('shop')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count,C("pagenum"));
        $lists = M('shop')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('id DESC,ord ASC')->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("keywords", $keywords);
        $this->display();
    }

    public function detail() {
        $id = I('get.id', '0', 'int');
        $detail = M("shop")->where("id =" . $id . "")->find();
        if ($id == 0) {
            $detail['is_check'] = 1;
            $detail['sendprice'] = 0;
            $detail['weeks'] = "1,2,3,4,5,6,7";
            $detail['is_start_phone'] = 1;
        }
        //星期
        $china = array("1" => "一", "2" => "二", "3" => "三", "4" => "四", "5" => "五", "6" => "六", "7" => "日");
        for ($i = 1; $i <= 7; $i++) {
            $weeks[] = $china[$i];
        }
        for ($i = 0; $i <= 23; $i++) {
            $hours[] = $i;
        }
        for ($i = 0; $i <= 11; $i++) {
            $minutes[] = $i * 5;
        }
        $business_time_arr = explode(",",$detail['business_time']);
        $detail['business_time_start2'] = $business_time_arr[4];
         $this->assign("sendtimeArr", getSendTime(0,0));
        $this->assign("detail", $detail);
        $this->assign("weeks", $weeks);
        $this->assign("hours", $hours);
        $this->assign("minutes", $minutes);
        $this->assign("dish_types", getDictionary('dishtype', 0));
        if ($detail['circles']) {
            $this->assign("circles", explode(",", $detail['circles']));
        }
        $this->assign("email", json_decode(getTableField("emails", "three"), true));
        $this->display();
    }

    public function detail_post() {
        $id = I('post.id', '', 'int');
        $data['is_start_phone'] = I("post.is_start_phone",0,'int');//是否开启店铺电话接受短信
        $data['name'] = I("post.name");
        $data['dish_type'] = I("post.dish_type");
        $day = I("post.day");
        $sendtimes = I("post.sendtimes");
        if (!empty($day)) {
            $data['weeks'] = implode(",", $day);
        }
        $data['sendtimes'] = implode(",", $sendtimes);
       $data['business_time'] = I("post.business_time");
        $data['sendtime'] = I("post.sendtime");
        $data['sendprice'] = I("post.sendprice");
        $data['startprice'] = I("post.startprice");
        $data['announce'] = I("post.announce");
        $data['addr'] = I("post.addr");
        $data['star'] = I("post.star");
        $data['ord'] = I("post.ord",0,30);
        $data['keywords'] = I("post.keywords");
        $data['description'] = I("post.description");
        $data['is_check'] = I('post.is_check', '0', 'int');
        $data['sendprice'] = I('post.sendprice', '0', 'int');
        $data['phone'] = I("post.phone");
        $data['email'] = I("post.email");
        $choseAllArea = I("post.choseAllArea");
        if ($choseAllArea != 'on') {
            $circles = I("post.circles");
                if ($circles) {
                $circles = array_unique(explode(",", $circles));
                $data['circles'] = implode(",", $circles);
            }
        } else {
            $data['circles'] = 0;
        }
        $sales = I("post.sales");
        if ($sales) {
            $data['sales'] = implode(",", $sales);
            if (in_array(1, $sales)) {
                $data['sales_full'] = I("post.sales_full");
                $data['sales_cut'] = I("post.sales_cut");
            } else {
                $data['sales_full'] = 0;
                $data['sales_cut'] = 0;
            }
            if (in_array(5, $sales)) {
                $data['sales_donate'] = I("post.sales_donate");
            } else {
                $data['sales_donate'] = '';
            }
        } else {
            $data['sales_full'] = '';
            $data['sales_cut'] = '';
            $data['sales_donate'] = '';
        }
        $path = C('shopsFile');
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = C('upload_max_size'); // 设置附件上传大小
        $upload->rootPath = $path; // 设置附件上传目录
        $upload->saveName = array('uniqid', '');
        $upload->autoSub = false;
        $info = $upload->upload();
        if ($info) {
            $uploadInfo = $info['logo'];
            if ($uploadInfo) {
                $type = strtolower(substr(strrchr($uploadInfo['name'], '.'), 1)); //文件类型
                $data['logo'] = $uploadInfo['savename']; //数据库保存名
                if (in_array($type, explode(",", C("picTypes")))) {
                  $image = new \Think\Image();
                $image->open($path . $data['logo']);
                $image->thumb(43, 43, \Think\Image::IMAGE_THUMB_FIXED)->save($path . "43_" . $data['logo']);
                }
            }
         }
        if ($id > 0) {
             M("shop")->where("id = " . $id . "")->save($data);
        } else {
             M("shop")->add($data);
        }
        if ($id > 0) {
            $this->success('修改成功！',session('QUERY_STRING'));
        } else {
            $this->success('添加成功！', U('Shop/lists'));
        }
    }
    public function dishtype() {//餐式类别
        $this->assign("lists", getDictionary('dishtype', 0));
        $this->display();
    }
    public function dishtype_detail() {//餐式类别详情
        $id = I("get.id", 0, 'int');
        $this->assign("detail", getStaticInfo($id, "dictionary"));
        $this->display();
    }

    public function dishtype_detail_post() {
        $id = I("post.id", 0, 'int');
        $data['name'] = trim(I("post.name"));
        $data['ord'] = I("post.ord", 0, 'int');
        if ($id > 0) {
            M("dictionary")->where("id = " . $id . "")->save($data);
            $this->success("修改餐式类别成功！", session('QUERY_STRING'));
        } else {
            $data['code'] = 'dishtype';
            M("dictionary")->add($data);
            $this->success("添加餐式类别成功！", U("Shop/dishtype"));
        }
        clearTempFile();
    }
    public function messages() {
        $shops = M("shop")->order("ord ASC")->select();
        $sql = "1=1";
        $keyword = trim(I('get.keyword'));
        $shop_id = I('get.shop_id',0,'int');
        if ($keyword) {
            $sql .= " AND content like '%" . $keyword . "%'";
        }
        if ($shop_id >0) {
            $sql .= " AND sid = '" . $shop_id . "'";
        }
        $count = M('messages')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count,C("pagenum"));
        $lists = M('messages')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("keywords", $keywords);
        $this->assign("shops", $shops);
        $this->assign("shop_id", $shop_id);
        $this->display();
    }
    public function comments() {
        $shops = M("shop")->order("ord ASC")->select();
        $sql = "1=1";
        $keyword = trim(I('get.keyword'));
        $shop_id = I('get.shop_id',0,'int');
//        if ($keyword) {
//            $sql .= " AND content like '%" . $keyword . "%'";
//        }
        if ($shop_id >0) {
            $sql .= " AND sid = '" . $shop_id . "'";
        }
        $count = M('comment')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count,C("pagenum"));
        $lists = M('comment')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('addtime')->select();
       // echo M('comment')->getlastsql();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
//        $this->assign("keywords", $keywords);
        $this->assign("shops", $shops);
        $this->assign("shop_id", $shop_id);
        $this->display();
    }

}

?>
